package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.MyLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    //根据用户id获取所属信息
    //专辑信息里就有用户id
    @GetMapping("getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        System.out.println(userId);
        UserInfoVo userInfoVo = userInfoService.getUserInfoVoByUserId(userId);
        return Result.ok(userInfoVo);
    }

    @MyLogin(required = false)
    @Operation(summary = "判断用户是否购买声音列表")
    @PostMapping("userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable("userId") Long userId, @PathVariable("albumId") Long albumId, @RequestBody List<Long> trackIdList) {
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
        // 返回map 集合数据
        return Result.ok(map);
    }

    @GetMapping("userIsPaidAlbum/{albumId}/{userId}")
    public Result<Boolean> userIsPaidAlbum(@PathVariable("albumId") Long albumId, @PathVariable("userId") Long userId) {
        return Result.ok(userInfoService.userIsPaidAlbum(albumId, userId));
    }

    @MyLogin
    @Operation(summary = "根据专辑id获取用户支付过的声音id列表")
    @GetMapping("findUserPaidTrackList/{albumId}")
    public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        // 根据用户Id 与 专辑Id 获取到已购买的声音Id 集合列表
        return Result.ok(userInfoService.findUserPaidTrackList(userId, albumId));
    }

    //添加购买记录
    @Operation(summary = "添加购买记录")
    @PostMapping("savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }
}

